package com.villaday.idf.activity3;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.custom.pop.FaPiaoInfoPop;
import com.villaday.idf.model3.FaPiaoconfigurationsEntity;
import com.villaday.idf.model3.Orders_invoice_deticalEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.StatusBarUtils;
import com.villaday.idf.util.ToastUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/*
 * 查看发票
 *
 * */
public class CheckInvoiceActivity extends BaseActivity {

    @BindView(R.id.toolbar_text)
    Toolbar toolbarText;
    @BindView(R.id.img_state)
    ImageView imgState;
    @BindView(R.id.tv_state)
    TextView tvState;
    @BindView(R.id.tv_order_state)
    TextView tvOrderState;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R.id.tv_fapiao_type)
    TextView tvFapiaoType;
    @BindView(R.id.tv_look_fapiao)
    TextView tvLookFapiao;
    @BindView(R.id.tv_order_content)
    TextView tvOrderContent;
    @BindView(R.id.tv_order_taitou)
    TextView tvOrderTaitou;
    @BindView(R.id.tv_jindu)
    TextView tvJindu;
    @BindView(R.id.img_0)
    ImageView img0;
    @BindView(R.id.tv_0)
    TextView tv0;
    @BindView(R.id.ll_state0)
    LinearLayout llState0;
    @BindView(R.id.view_state0)
    View viewState0;
    @BindView(R.id.img_1)
    ImageView img1;
    @BindView(R.id.tv_1)
    TextView tv1;
    @BindView(R.id.ll_state1)
    LinearLayout llState1;
    @BindView(R.id.view_state1)
    View viewState1;
    @BindView(R.id.img_2)
    ImageView img2;
    @BindView(R.id.tv_2)
    TextView tv2;
    @BindView(R.id.tv_send)
    TextView tvSend;
    @BindView(R.id.rv_button)
    RelativeLayout rvButton;
    private LoadingDialog loadingDialog;
    private Gson gson = new GsonBuilder().create();
    private Orders_invoice_deticalEntity.BodyBean body;

    @Override
    protected int getViewResId() {
        return R.layout.activity_check_invoice;
    }

    @Override
    protected void init() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            StatusBarUtils.setStatusBarLightMode(this, Color.parseColor("#FFF7F9FB"));
        ToolBarUtil.initToolBar(toolbarText, "发票详情", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        }, "发票信息", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//发票弹窗
                loadingDialog = LoadingDialog.newInstance("加载中");
                loadingDialog.show(getFragmentManager(), "loading");
                HashMap<String, String> map = new HashMap<>();
                map.put("key", "invoice_info");
                OkHttpUtil.getJSON(CheckInvoiceActivity.this, Constant.URL.faPiaoconfigurations, map, new OkHttpUtil.OnDataListener() {
                    @Override
                    public void onResponse(String url, String json) {
                        dismissLoading();
                        if (json != null) {
                            LogeUtil.e("faPiaoconfigurations", json);
                            FaPiaoconfigurationsEntity faPiaoconfigurationsEntity = gson.fromJson(json, FaPiaoconfigurationsEntity.class);
                            if (Constant.Strings.SUC.equals(faPiaoconfigurationsEntity.getCode())) {
                                FaPiaoInfoPop faPiaoInfoPop = new FaPiaoInfoPop(CheckInvoiceActivity.this, CheckInvoiceActivity.this, faPiaoconfigurationsEntity.getBody().getContent());
                                faPiaoInfoPop.showPopupWindow();
                            }
                        }

                    }

                    @Override
                    public void onFailure(String url, String error) {
                        dismissLoading();
                    }
                });
            }
        });

    }

    @Override
    protected void loadData() {
        loadingDialog = LoadingDialog.newInstance("加载中...");
        loadingDialog.show(getFragmentManager());
        Map<String, String> map = new HashMap<>();
        map.put("orderNo", getIntent().getStringExtra("orderNo"));
        OkHttpUtil.getJSON(this, Constant.URL.orders_invoice_detical, map, new OkHttpUtil.OnDataListener() {
            @Override
            public void onResponse(String url, String json) {
                dismissLoading();

                if (!TextUtils.isEmpty(json)) {
                    LogeUtil.e("orders_invoice_detical", json);
                    Orders_invoice_deticalEntity orders_invoice_deticalEntity = gson.fromJson(json, Orders_invoice_deticalEntity.class);
                    if (Constant.Strings.SUC.equals(orders_invoice_deticalEntity.getCode())) {
                        body = orders_invoice_deticalEntity.getBody();


                        Glide.with(CheckInvoiceActivity.this).load(body.getState().getIcon()).into(imgState);
                        tvState.setText(body.getState().getText());
                        tvOrderState.setText(body.getOrder().getState().getText());
                        tvOrderNum.setText(body.getOrder().getOrderNo());
                        tvOrderTime.setText(body.getOrder().getCreateDateTime());
                        // tvFapiaoType.setText(body.getOrder().getState());
                        tvOrderContent.setText(body.getContent());
                        tvOrderTaitou.setText(body.getTitle());

                        if (!body.isCompleted()) {//  "completed": "boolean,发票是否已开票",
                            tvLookFapiao.setVisibility(View.GONE);

                            img2.setSelected(true);
                            viewState1.setBackgroundColor(Color.parseColor("#E1E1E1"));
                            tv2.setSelected(true);

                        } else {
                            tvLookFapiao.setVisibility(View.VISIBLE);

                            img2.setSelected(false);
                            viewState1.setBackgroundColor(Color.parseColor("#ff7ed321"));
                            tv2.setSelected(false);
                        }

                        if (body.isAllowSendMail()) {//是否允许 发短信
                            rvButton.setVisibility(View.VISIBLE);

                        } else {
                            rvButton.setVisibility(View.GONE);
                        }


                    } else {
                        ToastUtil.initToast(CheckInvoiceActivity.this, orders_invoice_deticalEntity.getMsg());

                    }

                }
            }

            @Override
            public void onFailure(String url, String error) {
                dismissLoading();
                ToastUtil.initToast(CheckInvoiceActivity.this, error);
            }
        });
    }

    private void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }


    @OnClick({R.id.tv_look_fapiao, R.id.tv_order_content, R.id.tv_order_taitou, R.id.tv_send})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_order_content:
                break;
            case R.id.tv_order_taitou:
                break;
            case R.id.tv_send: {
                Intent intent = new Intent(this, FapiaoSendEmailActivity.class);
                intent.putExtra("orderNo", getIntent().getStringExtra("orderNo"));

                intent.putExtra("email", body.getEmail());

                startActivity(intent);
            }
            break;
            case R.id.tv_look_fapiao://查看发票

                if (body != null) {
                    Intent intent = new Intent(CheckInvoiceActivity.this, InvoiceDetailsActivity.class);
                    intent.putExtra("data", body);
                    startActivity(intent);
                }
                break;
        }
    }
}
